from  rest_framework import serializers
from .models import SaltMaster
from .models import Host
from .models import SaltAuth
import logging
logger = logging.getLogger('django')


class SaltMasterSeralizer(serializers.ModelSerializer):
    '''
    SaltMaster 序列化器
    '''
    class Meta:
        model = SaltMaster
        fields='__all__'


    def create(self, validated_data):
        request = self.context['request']
        validated_data['create_by'] =request.user.username
        saltmaster = SaltMaster(**validated_data)
        saltmaster.save()
        return saltmaster
    def update(self, instance, validated_data):
        request = self.context['request']
        instance.saltm_name = validated_data['saltm_name']
        instance.saltm_url = validated_data['saltm_url']
        instance.saltm_user = validated_data['saltm_user']
        instance.saltm_passwd = validated_data['saltm_passwd']
        instance.saltm_status = validated_data['saltm_status']
        instance.update_by = request.user.username
        instance.save()
        return instance



class HostSeralizer(serializers.ModelSerializer):
    '''
    主机 序列化器
    '''
    class Meta:
        model = Host
        fields='__all__'



    def create(self, validated_data):
        request = self.context['request']
        validated_data['create_by'] = request.user.username
        saltmaster = Host(**validated_data)
        saltmaster.save()
        return saltmaster

    def update(self, instance, validated_data):
        request = self.context['request']
        instance.lan = validated_data['lan']
        instance.name = validated_data['name']
        instance.wlan = validated_data['wlan']
        instance.system = validated_data['system']
        instance.status = validated_data['status']
        instance.update_by = request.user.username
        instance.save()
        return instance



class SaltAuthSeralizer(serializers.ModelSerializer):
    '''
    Salt 序列化器
    '''
    class Meta:
        model = SaltAuth
        fields='__all__'


    def create(self, validated_data):
        request = self.context['request']
        validated_data['create_by'] = request.user.username
        saltmaster = SaltAuth(**validated_data)
        saltmaster.save()
        return saltmaster

    def update(self, instance, validated_data):
        request = self.context['request']
        instance.saltname = validated_data['saltname']
        instance.salt_master = validated_data['salt_master']
        instance.salt_status = validated_data['salt_status']
        instance.salt_bind_status = validated_data['salt_bind_status']
        instance.salt_isvalidate = validated_data['salt_isvalidate']
        instance.update_by = request.user.username
        instance.save()
        return instance


